﻿@using Viettel.VOFFICE.Web.Models
<link href="@Url.Content("~/Scripts/treeview/jquery.treeview.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/treeview/jquery.treeview.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var $deptTree = $('#dept-tree');
        $deptTree.treeview({
            collapsed: true
        });
        $('.hitarea').first().trigger('click');

        $('.show-dept').bind('click', function (e) {
            var $target = $(e.currentTarget);
            //console.log($target);
            window.SelectedDeptId = $target.attr('data-id');
            window.SelectedDeptName = $target.find('span').first().text();
            $('body').trigger('closeSDT');
        });
    });
</script>
<ul id="dept-tree">
 @{
    if (ViewBag.DeptTree != null)
    {
        var list = ViewBag.DeptTree as List<DepartmentModel>;
        if (list != null && list.Any())
        {
            var print = "";
            var openUl = 0;
            var openLi = 0;
            for (var i = 0; i < list.Count(); i++ )
            {
                var item = list[i];
                if (i == 0 && (item.ParentId == 0 || item.ParentId == null) && item.Node == 0)
                {
                    print = "<li> <a class = 'show-dept' data-id = '"+ item.Id +"' ><span title='"+ item.Description +"'>"+ @item.Name + "</span></a>";
                    openLi++;
                    @Html.Raw(print);
                    continue;
                }
                if(i > 0 && item.Node == list[i-1].Node){
                    print = "</li><li> <a class = 'show-dept' data-id = '" + item.Id + "' ><span title='" + item.Description + "'>" + @item.Name + "</span></a>";
                    @Html.Raw(print);
                    continue;                    
                }
                if(i > 0 && item.Node > list[i-1].Node){
                    print = "<ul><li> <a class = 'show-dept' data-id = '" + item.Id + "' ><span title='" + item.Description + "'>" + @item.Name + "</span></a>";
                    openLi++;
                    openUl++;
                    @Html.Raw(print);
                    continue;
                }
                if(i > 0 && item.Node < list[i-1].Node){
                    print = "</li>";
                    openLi--;
                    @Html.Raw(print);
                    var k = list[i - 1].Node - item.Node;
                    while (openUl > 0 && k > 0)
                    {
                        print = "</ul>";
                        openUl--;
                        @Html.Raw(print);
                        if(openLi > 0){
                            print = "</li>";
                            openLi--;
                            @Html.Raw(print); 
                        }
                        k--;
                    }
                    print = "<li> <a class = 'show-dept' data-id = '" + item.Id + "' ><span title='" + item.Description + "'>" + @item.Name + "</span></a>";
                    openLi++;
                    @Html.Raw(print);
                    continue;                                                       
                }
            }
            if(openLi > 0){
                print = "</li>";
                openLi--;
                @Html.Raw(print);
            }
            while (openUl > 0)
            {
                print = "</ul>";
                openUl--;
                @Html.Raw(print);
                if (openLi > 0)
                {
                    print = "</li>";
                    openLi--;
                    @Html.Raw(print); 
                }          
            }
        }
    }    
}
</ul>